package store.lunangangster.nodeflow.parser.el.operator;

import com.ql.util.express.Operator;
import store.lunangangster.nodeflow.exception.RuleSourceParseException;
import store.lunangangster.nodeflow.rt.element.flow.WhenFlow;
import store.lunangangster.nodeflow.util.Converter;

/**
 * 用于扩展WhenFlow的any操作符
 *
 * @author cwk
 * @version 1.0
 */
public class AnyOperator extends Operator {

  /**
   * WHEN(x).any(true)执行完之后，返回值依然是WhenFlow
   * @param list        交换列表：a.any(b)和any(a, b)所持有的交换列表为：[a, b]
   * @return            WhenFlow对象
   */
  @Override
  public Object executeInner(Object[] list) {
    if (list.length < 1 || list.length > 2) {
      throw new RuleSourceParseException("Incompatible attribute for any: should be like WHEN().any() or WHEN().any(true/false)");
    }
    WhenFlow whenFlow = Converter.convert(list[0], WhenFlow.class);
    if (list.length == 1) {
      whenFlow.setAny(true);
    } else {
      whenFlow.setAny(Converter.convert(list[1], Boolean.class));
    }
    return whenFlow;
  }
}
